from abc import abstractmethod, ABCMeta

"""
监听模式
简介:
https://weibo.com/2843149595/JvhHnpMAy
"""


class Observer(metaclass=ABCMeta):
    """ Interface """

    @abstractmethod
    def update(self, object):
        pass


class Observable(object):
    def __init__(self, temperature):
        self._observers = []
        self._temperature = temperature

    def add_observer(self, observer):
        self._observers.append(observer)

    def remove_observer(self, observer):
        self._observers.remove(observer)

    def get_temperature(self):
        return self._temperature

    def notisify(self):
        for o in self._observers:
            o.update(self._temperature)

    def update_tempture(self, val):
        self._temperature = val
        print('水温是: %d' % val)
        self.notisify()


class WashingHeater(Observer):
    """Observer for washing"""

    def update(self, object):
        if object >= 50 and object <= 70:
            print('可以洗澡了\n')


class BoilingHeater(Observer):
    """Observer for boiling water"""

    def update(self, object):
        if object == 100:
            print('水烧开了,可以喝了\n')


if __name__ == '__main__':
    obj = Observable(30)
    washing = WashingHeater()
    boiling = BoilingHeater()

    obj.add_observer(washing)
    obj.add_observer(boiling)
    obj.update_tempture(50)
    obj.update_tempture(70)
    obj.update_tempture(100)
